﻿#pragma once
#include <iostream>
#include <vector>
#include <assert.h>
using namespace std;

// AVLTreeNode
template<class K, class V>
struct AVLTreeNode
{
	pair<K, V> _kv;    // data
	AVLTreeNode<K, V>* _left;    // left ptr
	AVLTreeNode<K, V>* _right;   // right ptr
	AVLTreeNode<K, V>* _parent;  // parent ptr
	int _bf;    // balance factor

	// struct
	AVLTreeNode(const pair<K, V>& kv)
		:_kv(kv)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _bf(0)    // new node bf = 0
	{};
};

//AVLTree
template<class K, class V>
class AVLTree
{
	using Node = AVLTreeNode<K, V>;
public:
	// Insert
	bool Insert(const pair<K, V>& kv)
	{
		// AVLTree is null
		if (nullptr == _root)
		{
			_root = new Node(kv);
			return true;
		}

		// AVLTree is not null
		Node* parent = nullptr;    // parent Node
		Node* cur = _root;
		// find the Insert position
		while (cur)
		{
			if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else {
				return false;
			}
		}
		// ifndef false, find the position
		cur = new Node(kv);
		// determine the cur position
		if (parent->_kv.first < kv.first)
			parent->_right = cur;
		else
			parent->_left = cur;
		cur->_parent = parent;
		// balance factor question
		while (parent)
		{
			// update balance factor
			// balance factor = right - left;
			if (cur == parent->_left)
				parent->_bf--;
			else
				parent->_bf++;

			// determine bf
			if (parent->_bf == 0)
				break;
			else if (parent->_bf == 1 || parent->_bf == -1)
			{
				// continue update
				cur = parent;
				parent = parent->_parent;
			}
			else if (parent->_bf == 2 || parent->_bf == -2)
			{
				// need rotate to adjust balance
				if (parent->_bf == -2 && cur->_bf == -1)// [1] RotateR: left height
					RotateR(parent);
				else if (parent->_bf == 2 && cur->_bf == 1)// [2] RotateL: right height
					RotateL(parent);
				else if (parent->_bf == -2 && cur->_bf == 1)
					RotateLR(parent);
				else if (parent->_bf == 2 && cur->_bf == -1)
					RotateRL(parent);
				else
					assert(false);
				break;
			}
			else
				assert(false);
		}
		return true;
	}

	void InOrder()
	{
		_Inorder(_root);
	}

	bool IsBalanceTree()
	{
		return _IsBalanceTree(_root);
	}
private:

	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		// subLR and parent
		parent->_left = subLR;
		if(subLR) // if subLR == nullptr ! nullptr visit_eorr
			subLR->_parent = parent;

		// subL and parent
		Node* pparent = parent->_parent;
		subL->_right = parent;
		parent->_parent = subL;

		// determine parent is ? _root : Node
		if (parent == _root)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else {
			// determine parent is ? pparent->left : pparent->right
			if (pparent->_left == parent)
				pparent->_left = subL;
			else
				pparent->_right = subL;
		}
		subL->_bf = parent->_bf = 0;
	}

	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		// parent and subRL
		parent->_right = subRL;
		if(subRL)
			subRL->_parent = parent;

		// parent and subR
		Node* pparent = parent->_parent;
		subR->_left = parent;
		parent->_parent = subR;

		// determine parent is ? _root : Node
		if (parent == nullptr)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else {
			// determine parent is ? pparent->left : pparent->right
			if (pparent->_left == parent)
				pparent->_left = subR;
			else
				pparent->_right = subR;
		}
		subR->_bf = parent->_bf = 0;
	}

	void RotateLR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		int bf = subLR->_bf;
		RotateL(subL);
		RotateR(parent);

		if (bf == 0)
		{
			subL->_bf = 0;
			subLR->_bf = 0;
			parent->_bf = 0;
		}
		else if (bf == 1)
		{
			subL->_bf = -1;
			subLR->_bf = 0;
			parent->_bf = 0;
		}
		else if (bf == -1)
		{
			subL->_bf = 0;
			subLR->_bf = 0;
			parent->_bf = 1;
		}
		else {
			assert(false);
		}
	}

	void RotateRL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		int bf = subRL->_bf;
		
		RotateR(subR);
		RotateL(parent);

		if (bf == 0)
		{
			subR->_bf = 0;
			subRL->_bf = 0;
			parent->_bf = 0;
		}
		else if (bf == 1)
		{
			subR->_bf = 0;
			subRL->_bf = 0;
			parent->_bf = -1;
		}
		else if (bf == -1)
		{
			subR->_bf = 1;
			subRL->_bf = 0;
			parent->_bf = 0;
		}
		else {
			assert(false);
		}
	}

	void _Inorder(Node* _root)
	{
		if (_root == nullptr)
			return;
		_Inorder(_root->_left);
		cout << _root->_kv.first << ":" << _root->_kv.second << endl;
		_Inorder(_root->_right);
	}

	int _Height(Node* root)
	{
		if (root == nullptr)
			return 0;
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
	}

	bool _IsBalanceTree(Node* root)
	{
		// 空树也是AVL树
		if (nullptr == root)
			return true;
		// 计算pRoot结点的平衡因⼦：即pRoot左右⼦树的⾼度差
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		int diff = rightHeight - leftHeight;
		// 如果计算出的平衡因⼦与pRoot的平衡因⼦不相等，或者
		// pRoot平衡因⼦的绝对值超过1，则⼀定不是AVL树
		if (abs(diff) >= 2)
		{
			cout << root->_kv.first << "高度差异常" << endl;
			return false;
		} 
		if(root->_bf != diff)
		{
			cout << root->_kv.first << "平衡因⼦异常" << endl;
			return false;
		} 
		// pRoot的左和右如果都是AVL树，则该树⼀定是AVL树
			return _IsBalanceTree(root->_left) && _IsBalanceTree(root->_right);
	}
	Node* _root = nullptr;
};